﻿/*
 * Copyright (c) 2013 Lambert van Lieshout, YUMMO Software Development
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
*/

namespace Yummo.BlackBox
{
    using System;

    /// <summary>
    /// BlackBox utilities.
    /// </summary>
    public static class BlackBoxUtilities
    {
        /// <summary>
        /// Converts event types to a comma seperated string format.
        /// </summary>
        /// <param name="value">EventTypes enumerator.</param>
        /// <returns>Event types in comma seperated string format.</returns>
        public static string GetWriteLevelToString(EventTypes value)
        {
            string eventTypeString = "";
            if ((short)value == 0) return ((EventTypes)0).ToString();
            foreach (EventTypes eventType in Enum.GetValues(typeof(EventTypes)))
                if (((short)value & (short)eventType) == (short)eventType)
                    eventTypeString += eventType.ToString() + ",";
            return eventTypeString.ToLower().TrimEnd(new char[] { ',' });
        }

        /// <summary>
        /// Converts a comma seperated string with event types to EventTypes enum format.
        /// </summary>
        /// <param name="value">Comma seperated string with event types.</param>
        /// <returns>EventTypes enumerator.</returns>
        public static EventTypes GetWriteLevelFromString(string value)
        {
            if (String.IsNullOrEmpty(value)) return 0; // EventTypes.Critical & EventTypes.Debug & EventTypes.Error & EventTypes.Trace & EventTypes.Warning;
            EventTypes result = 0;
            value = value.Replace(" ", "").ToLower();
            foreach (EventTypes eventType in Enum.GetValues(typeof(EventTypes)))
                if (value.IndexOf(eventType.ToString().ToLower()) >= 0)
                    result = result | eventType;
            return result;
        }

        /// <summary>
        /// Get all event types in the EvenTypes enumerator.
        /// </summary>
        /// <returns></returns>
        public static EventTypes GetAllEventTypes()
        {
            EventTypes allEventTypes = 0;
            foreach (EventTypes eventType in Enum.GetValues(typeof(EventTypes)))
                allEventTypes = allEventTypes | eventType;
            return allEventTypes;
        }
    }
}
